package edu.spec.student.controller;

import edu.spec.student.model.Message;
import edu.spec.student.model.Student;
import edu.spec.student.service.iface.IMessageService;
import edu.spec.student.service.iface.IStudentService;
import edu.spec.basic.model.dto.MvcReturn;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/message")
public class MessageController {

    @Inject
    private IMessageService messageService;
    @Inject
    private IStudentService studentService;

    /**
     * 学生写信
     * @param message 学生提交的数据转化成对应的POJO
     * @return  返回信息
     */
    @ResponseBody
    @RequestMapping("/student/send")
    public MvcReturn send(Message message, HttpSession session){
        Student stu = (Student)session.getAttribute("user");
        message.setStudent(studentService.load(stu.getId()));
        message.setIfStudentUpdate(1);
        message.setMessageDate(new Date());
        messageService.add(message);
        return new MvcReturn(true);
    }

    /**
     * 学生补充信息
     * @param messageId     信件的主键
     * @param supplement    补充信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/supplement")
    public MvcReturn supplement(Integer messageId, String supplement){
        Message parent = messageService.load(messageId);
        parent.setIfStudentUpdate(1);
        parent.setMessageDate(new Date());

        Message m = new Message();
        m.setParent(parent);
        m.setStudentMessage(supplement);
        m.setMessageDate(new Date());
        messageService.add(m);
        return new MvcReturn(true);
    }

    /**
     * 主任回复
     * @param messageId     信件的主键
     * @param respnose      主任回复
     * @return  返回信息
     */
    @ResponseBody
    @RequestMapping("/respnose")
    public MvcReturn respnose(Integer messageId, String respnose){
        Message parent = messageService.load(messageId);
        parent.setIfDirectorUpdate(1);
        parent.setResponseDate(new Date());

        Message m = new Message();
        m.setParent(parent);
        m.setDirectorResponse(respnose);
        m.setResponseDate(new Date());
        messageService.add(m);
        return new MvcReturn(true);
    }

    /**
     * 主任使用
     * 加载最近更新邮件，根据学生最后更新时间排序
     * 分页
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByMessageDate")
    public MvcReturn findByMessageDate(){
        Pager<Message> m = messageService.findByMessageDate();
        return new MvcReturn(true, m.getDatas(), m.getTotal());
    }

    /**
     * 学生使用
     * 加载学生的邮件
     * 不分页，学生不会写太多的邮件
     * @return
     */
    @ResponseBody
    @RequestMapping("/listByStudent")
    public MvcReturn listByStudent(HttpSession session){
        Student stu = (Student)session.getAttribute("user");
        List<Message> mList = messageService.listByStudent(stu.getId());
        return new MvcReturn(true, mList);
    }
}
